package com.neu.ui.home;

import android.app.DownloadManager;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.android.volley.Request;
import com.google.gson.Gson;
import com.neu.bean.Rotation;
import com.neu.bean.ServiceList;
import com.neu.bean.Weather;
import com.neu.util.VolleyUtil;

public class HomeViewModel extends ViewModel {
    //定义liveData数据
    private MutableLiveData<Rotation> mRotation;
    private MutableLiveData<ServiceList> mServiceList;
    private MutableLiveData<Weather> mWeather;


    private Gson gson;

    public HomeViewModel(){
        mRotation=new MutableLiveData<>();
        mServiceList=new MutableLiveData<>();
        mWeather=new MutableLiveData<>();
        gson=new Gson();
    }

    //获得天气信息
    public LiveData<Weather> getWeather(){
        String url="/prod-api/api/common/weather/today";
        VolleyUtil.request(Request.Method.GET, url, null, new VolleyUtil.BackJson() {
            @Override
            public void success(String json) {
                Weather weather=gson.fromJson(json,Weather.class);
                mWeather.setValue(weather);
            }
        });
        return mWeather;
    }


    //获得全部服务数据
    public LiveData<ServiceList> getServiceList(){
        String url="/prod-api/api/service/list";
        VolleyUtil.request(Request.Method.GET, url, null, new VolleyUtil.BackJson() {
            @Override
            public void success(String json) {
                ServiceList serviceList=gson.fromJson(json,ServiceList.class);
                mServiceList.setValue(serviceList);
            }
        });
        return mServiceList;
    }


    //获得轮播数据
    public LiveData<Rotation> getRoation(){
        String url="/prod-api/api/rotation/list?pageNum=1&pageSize=8&type=2";
        VolleyUtil.request(Request.Method.GET, url, null, new VolleyUtil.BackJson() {
            @Override
            public void success(String json) {
                Rotation rotation=gson.fromJson(json,Rotation.class);

                //将数据放入到lviedata中
                mRotation.setValue(rotation);
            }
        });

        return mRotation;
    }

}